import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


public class ModifyPointFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textFieldPoint;
	private JComboBox<String> cmbbxUser;
	private JRadioButton rdbtnRMB;
	private JRadioButton rdbtnYXB;
	/**
	 * Create the frame.
	 */
	public ModifyPointFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("姓名");
		label.setFont(new Font("Dialog", Font.BOLD, 20));
		label.setBounds(39, 12, 60, 29);
		contentPane.add(label);
		
		cmbbxUser = new JComboBox<String>();
		cmbbxUser.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				initialType();
			}
		});
		cmbbxUser.setFont(new Font("Dialog", Font.BOLD, 20));
		cmbbxUser.setBounds(117, 16, 155, 24);
		contentPane.add(cmbbxUser);
		
		JLabel label_2 = new JLabel("积分");
		label_2.setFont(new Font("Dialog", Font.BOLD, 20));
		label_2.setBounds(39, 65, 60, 29);
		contentPane.add(label_2);
		
		textFieldPoint = new JTextField();
		textFieldPoint.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char keyChar = e.getKeyChar();
				if(!((keyChar >= '0' && keyChar <= '9'))){
					e.consume();
				}
			}
		});
		textFieldPoint.setFont(new Font("Dialog", Font.PLAIN, 20));
		textFieldPoint.setBounds(117, 65, 148, 29);
		contentPane.add(textFieldPoint);
		textFieldPoint.setColumns(10);
		
		JButton btnModify = new JButton("修改");
		btnModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String name =(String)cmbbxUser.getSelectedItem();
				String value = textFieldPoint.getText();
				int points;
				if(value.length() > 0){
					points = Integer.valueOf(value);
				}else{
					points = 0;
				}
				int type = 0;
				if(rdbtnRMB.isSelected()){
					type = 1;
				}else if(rdbtnYXB.isSelected()){
					type = 0;
				}
				Launcher.updatePointByName(name,points,type);
				JOptionPane.showMessageDialog(null, "更改成功");
			}
		});
		btnModify.setFont(new Font("Dialog", Font.BOLD, 20));
		btnModify.setBounds(48, 161, 117, 25);
		contentPane.add(btnModify);
		
		JButton btnReturn = new JButton("返回");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				Launcher.showMainFrame();
			}
		});
		btnReturn.setFont(new Font("Dialog", Font.BOLD, 20));
		btnReturn.setBounds(213, 161, 117, 25);
		contentPane.add(btnReturn);
		
		rdbtnRMB = new JRadioButton("人民币");
		rdbtnRMB.setBounds(283, 17, 72, 23);
		contentPane.add(rdbtnRMB);
		
		rdbtnYXB = new JRadioButton("游戏币");
		rdbtnYXB.setBounds(283, 70, 72, 23);
		contentPane.add(rdbtnYXB);
		
		ButtonGroup group = new ButtonGroup();
		group.add(rdbtnYXB);
		group.add(rdbtnRMB);
	}
	
	public void setNameList(String[] names){
		cmbbxUser.setModel(new DefaultComboBoxModel<String>(names));
	}
	public void initialType(){
		String name =(String)cmbbxUser.getSelectedItem();
		User user = Launcher.getUserByName(name);
		if(user.getType() == 0){
			rdbtnYXB.setSelected(true);
		}else if(user.getType() == 1){
			rdbtnRMB.setSelected(true);
		}
		textFieldPoint.setText(null);
	}
	
	protected void processWindowEvent(WindowEvent e){
		if(e.getID() == WindowEvent.WINDOW_CLOSING){
		}else{
			super.processWindowEvent(e);
		}
	}
}
